﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using FlickrSample.SL.Infrastructure.Flickr.Entities;

namespace FlickrSample.SL.Infrastructure.Flickr.Parser
{
    public static class UserParser
    {
        public static FlickrUser ParseSimple(string userXml)
        {
            if (!Common.CheckError(userXml))
            {
                var doc = XDocument.Parse(userXml);

                var user = new FlickrUser();

                user.NSID = doc.Root.Element("user").Attribute("nsid").Value;
                user.UserName = doc.Root.Element("user").Element("username").Value;

                return user;
            }

            return null;

        }

        public static bool ParseDetail(FlickrUser user, string userXml)
        {
            if (Common.CheckError(userXml))
            {
                return false;
            }

            var doc = XDocument.Parse(userXml);
            XElement person = doc.Root.Element("person");

            
            user.IsPro = person.Attribute("ispro").Value == "1";

            user.UserName = person.Element("username").Value;
            user.RealName = person.Element("realname").Value;

            user.PhotosUrl = new Uri(person.Element("photosurl").Value);
            user.ProfileUrl = new Uri(person.Element("profileurl").Value);
            user.PhotoCount = Convert.ToInt32(person.Element("photos").Element("count").Value);

            string iconfarm = person.Attribute("iconfarm").Value;
            string iconServer = person.Attribute("iconserver").Value;

            //Random rand = new Random(DateTime.Now.Millisecond);

            user.ProfilePictureUrl = new Uri(string.Format("http://farm{0}.static.flickr.com/{1}/buddyicons/{2}.jpg", iconfarm, iconServer, user.NSID), UriKind.Absolute);
            return true;
        }
    }
}
